<?php
/*
课程管理模块
*/
include_once('include/top.php');

$action=fs_get_value("do");
switch($action)
{
	case	"addcourse"	     : addcourse()	             ; break;
	case	"add"	             : do_addcourse()	     ; break;
	case	"delete"	     : delete_course()	     ; break;
	case    "searchcourse"   : searchcourse()            ; break;
	default                      : main()                        ; break;
}
?>

<script type="text/javascript">
function Check(){
    if(document.addform.title.value ==  ""){
        alert("课程名称不能为空!");
        document.addform.title.focus();
        return  false;
    }
    if(document.addform.teacherid.value ==  ""){
        alert("请选择主讲教师!");
        document.addform.teacherid.focus();
        return  false;
    }
    if(document.addform.state.value ==  ""){
        alert("请选择状态!");
        document.addform.state.focus();
        return  false;
    }
}
</script>

<?php

function main()
{
    global $db;
        $teacherid = fs_get_value("teacherid");
	$title = fs_get_value("title");
        $state = fs_get_value("state");
	$sort = fs_get_value("sort");
	$xuhao = fs_get_value("xuhao");
	$introduction = fs_get_value("introduction");
        $outline = fs_get_value("outline");
        $calendar = fs_get_value("calendar");
	$conditionarray = array($teacherid,$title,$state,$sort,$xuhao,$introduction,$outline,$calendar);
	$valuearray = array(" and teacherid = '".$teacherid."'"," and title like '%".$title."%'"," and state = '".$state."'"," and sort = ".$sort," and xuhao = ".$xuhao," and introduction like '%".$introduction."%'"," and outline like '%".$outline."%'"," and calendar like '%".$calendar."%'");
	$sql = "select * from nettteach_course where 1=1";
	for($i=0;$i<count($conditionarray);$i++ ){
		if($conditionarray[$i] == ''){
			$valuearray[$i] = '';
		}
		$sql.=$valuearray[$i];
	}
	$course_result = $db -> query($sql);
	$recordcount = $db -> num_rows($course_result);
	@$page=$_GET['p'];
	if ($page==null){$page=1;}
	$pagesize=15;								//每页显示数
	$pagecount=ceil($recordcount/$pagesize);	//计算总页
	if ($page>$pagecount){$page=$pagecount;}
	if ($page<1){$page=1;}
?>
<script src="./js/Main.js"></script>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="CContent">
  <tr>
    <th class="CTitle">课程管理</th>
  </tr>
  <tr>
    <td class="CPanel">
    <div class="SubMenu">
		<a href="admin_course.php?">课程管理</a> | 
		<a href="admin_course.php?do=addcourse">添加课程</a> | 
		<a href="admin_course.php?do=searchcourse">搜索课程</a>
	</div>
	
	<form action="?do=delete" method="post" name="fooeeform" style="margin:0px">
		<table border="0" cellpadding="0" cellspacing="0"  class="TablePanel" width="100%">
			<tr align="center" class="TDHead">
				<td nowrap><input type=button style="font-size:9pt;width:50px;" value="全选" onclick="click_allid(fooeeform,1)"/></td>
				<td nowrap><input type=button style="font-size:9pt;width:60px;" value="全取消" onclick="click_allid(fooeeform,0)"/></td>
				<td nowrap><input type="submit" style="font-size:9pt;width:50px;" value="删除" onclick="return delid()"/></td>

				<td nowrap width="500"><input type=button style="font-size:9pt;width:80px;" onclick="window.location.href('admin_course.php?do=searchcourse')" value="搜索课程" />
				</td>
				<td nowrap>.</td>
				<td nowrap>.</td>
				<td width="30%" nowrap></td>
				<td nowrap></td>
			</tr>
		</table>
<div id="click_idall">
    <div align="left" style="padding:5px;">
      <table border="0" cellpadding="0" cellspacing="0" class="TablePanel" align="center" width="100%">
        <tr align="center">
		  <td class="TDHead">√</td>
          <td class="TDHead" nowrap>课程名称</td>
<!--          <td class="TDHead" nowrap>课程分类</td>-->
          <td class="TDHead" nowrap>主讲教师</td>
          <td class="TDHead" nowrap>所属系/专业</td>
          <td class="TDHead" nowrap>用户名</td>
          <td class="TDHead" nowrap>课程状态</td>
          <td class="TDHead" nowrap>排列序号</td>
	  <td class="TDHead" nowrap>入库时间</td>
          <td class="TDHead" nowrap>操 作</td>
        </tr>
<?php
	$course_result=$db->query($sql." limit ".($page-1)*($pagesize).",$pagesize");
	while($course_row = $db->fetch_array($course_result))
	{
?>
		<tr id="t<?php echo($course_row["id"]);?>" onclick="click_tr('<?php echo ($course_row["id"]);?>')" onMouseOver="bgColor='#FF8899'" onMouseOut="bgColor='FFFFFF'" title=单击选中 align="center">
			<td><input name='id[]' type='checkbox' id="c<?php echo($course_row["id"]);?>" value='<?php echo ($course_row["id"]);?>' onclick="click_tr('<?php echo ($course_row["id"]);?>')"></td>
			<td><a href="?do=addcourse&course_id=<?php echo ($course_row["id"]);?>"><?php echo($course_row["title"]);?></a></td>
<!--                        <td><?php echo(fs_get_course_fenlei($course_row['sort']));?></td>-->
                        <td><?php echo(get_from_info('nettteach_member','realname',$course_row['teacherid']));?></td>
			<td><?php echo(fs_get_depart_major('nettteach_member',$course_row['teacherid']));?></td>
                        <td><?php echo(get_from_info('nettteach_member','passport',$course_row['teacherid']));?></td>
                        <td><?php echo(fs_get_state_view($course_row['state']));?></td>
			<td><?php echo($course_row['xuhao']);?></td>
			<td><?php echo(date("Y-m-d H:i:s",$course_row['intotime']));?></td>
			<td><a href="?do=addcourse&course_id=<?php echo ($course_row["id"]);?>">编缉</a></td>
		</tr>
		<tr>
			<td height=1 colspan=10 bgcolor="#E8F1FF" onMouseOver="bgColor='#FF8899'" onMouseOut="bgColor='E7sxE7sxE7sx'"></td>
		</tr>
<?php
	}
?>
	</table>
	
	
	
</div>
	</div>
	<center>
<?php
/*分页示例:

pager(传入URL , 数据总数 , 开始条数 , 每页数目)
*/
echo(pager('?p={page}',$recordcount,($page-1)*($pagesize),$pagesize));		
?>
</center>
	</td>
	</tr>
	</table>
</form>
<?php
}
?>


<!--
编缉课程开始
-->
<?php
function addcourse()
{
    global $db;
	$course_id = fs_get_value("course_id");
	if($course_id != ''){
		$sql = "select * from nettteach_course where id = ".$course_id;
		$course_result = $db -> query($sql);
		$course_row = $db -> fetch_array($course_result);
	}
?>

<script type="text/javascript">
window.onload = function()
{
        CKEDITOR.replace( 'introduction',{ height : '150px', width:'600px',toolbar:'MyToolbar',fontSize_sizes:'16/16px' } );
        CKEDITOR.replace( 'outline',{ height : '150px', width:'600px',toolbar:'MyToolbar',fontSize_sizes:'16/16px' } );
        CKEDITOR.replace( 'calendar',{ height : '150px', width:'600px',toolbar:'MyToolbar',fontSize_sizes:'16/16px' } );
};
</script>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="CContent">
		<tr>
		    <th class="CTitle">课程管理</th>
		</tr>	
		<tr>
			<td class="CPanel">
    <div class="SubMenu">
		<a href="admin_course.php?">课程管理</a> | 
		<a href="admin_course.php?do=addcourse">添加课程</a> | 
		<a href="admin_course.php?do=searchcourse">搜索课程</a>
	</div>
			</td>
		</tr>	
		<tr>
			<td>
			<div class="middle_right_list">
			<form name="addform" method="post" action="?do=add&course_id=<?php echo($course_id);?>" class="CContent">
			<fieldset>
				<legend>添加/编缉课程</legend>
				    <label></label>带<span style="color:#FF0000;">*</span>的必填。<br/><br/>
                                        <label><div align="right">主讲教师：</div></label>
                                        <select name="teacherid" style="width:120px;">
					    <option value="" >请选择主讲教师</option>
                                        <?php
                                                $sql_teacher = "select * from nettteach_member where type in('1','2')";
                                                $result_teacher = $db -> query($sql_teacher);
                                                while($row_teacher = $db -> fetch_array($result_teacher))
                                                {       
                                                        if($course_row['teacherid'] == $row_teacher['id'])
                                                        {
                                                                $i = "selected";
                                                        }else
                                                        {
                                                                $i = '';
                                                        }
                                                        echo  "<option ".$i." value = ".$row_teacher['id'].">".$row_teacher['realname']."</option>";
                                                }
                                        ?>
                                        </select><br/></br>
					<label><div align="right">课程名称：</div></label><input name="title" type="text" style="width:200px;" class="text" value="<?php echo($course_row['title']);?>" /><span style="color:#FF0000;">*</span><br/><br/>
<!--                                        <label><div align="right">课程分类：</div></label>
					<select name="sort" style="width:120px;">
					    <option value="0" <?php if($course_row['sort'] == 0){echo("selected");}?>>普通课程</option>
                                            <option value="1" <?php if($course_row['sort'] == 1){echo("selected");}?>>推荐课程</option>
					    <option value="2" <?php if($course_row['sort'] == 2){echo("selected");}?>>精品课程</option>
					</select><br/></br>-->
                                        <label><div align="right">课程状态：</div></label>
					<select name="state" style="width:120px;">
					    <option value="" <?php if($course_row['state'] == null){echo("selected");}?>>未知</option>
                                            <option value="0" <?php if($course_row['state'] == 0){echo("selected");}?>>显示</option>
					    <option value="1" <?php if($course_row['state'] == 1){echo("selected");}?>>不显示</option>
					</select><br/></br>
                                        
					<label><div align="right">排列序号：</div></label><input name="xuhao" type="text" style="width:400px;" class="text" value="<?php echo($course_row['xuhao']);?>"><br/><br/>
<!--					<label><div align="right">入库时间：</div></label><input name="intotime" type="text" style="width:400px;" class="text" value="<?php if($course_id == ''){echo(date("Y-m-d H:i:s",time()));}else{echo(date("Y-m-d H:i:s",$course_row['intotime']));}?>"><br/><br/>-->
					<label><div aling="right">课程介绍：</div></label><div style="padding-left:100px"><textarea name="introduction" style="width:600px;height:120px;"><?php echo($course_row['introduction']);?></textarea></div><br /><br/>
                                        <label><div aling="right">课程大纲：</div></label><div style="padding-left:100px"><textarea name="outline" style="width:600px;height:120px;"><?php echo($course_row['outline']);?></textarea></div><br /><br/>
                                        <label><div aling="right">课程日历：</div></label><div style="padding-left:100px"><textarea name="calendar" style="width:600px;height:120px;"><?php echo($course_row['calendar']);?></textarea></div><br /><br/>
					<input name="intotime" type="hidden" style="width:400px;" class="text" value="<?php if($course_id == ''){echo(date("Y-m-d H:i:s",time()));}else{echo(date("Y-m-d H:i:s",$course_row['intotime']));}?>">
                                        <div class="submit">
					<label></label><input type="submit" value="确认提交" onclick="return Check();"/>&nbsp;&nbsp;&nbsp;&nbsp;<a class="lj" href="admin_course.php">返回列表</a></div>
			</fieldset>
			</form>
			</div>
			</td>
		</tr>
	</table>
<?php
}
function do_addcourse(){
    global $db;
        $teacher_id = fs_get_value("teacherid");
	$title = fs_get_value("title");
	$xuhao = fs_get_value("xuhao");
	$intotime = fs_get_value("intotime");
//        $sort = fs_get_value("sort");
	$state = fs_get_value("state");
	$introduction = fs_get_value("introduction");
        $outline = fs_get_value("outline");
        $calendar = fs_get_value("calendar");
        if($teacher_id == '')
        {
                fs_showmsg("请选择教师!");
                exit();
        }
	//增加课程
	if(fs_get_value("course_id") == ''){
		if($title != ''){
			$sql = "insert into nettteach_course (title,teacherid) value ('".$title."','".$teacher_id."')";
			$db -> query($sql);
			$id = mysql_insert_id();
		}
	}
	else{
		//修改课程
		$id = fs_get_value("course_id");
		if($title != ''){
			$sql = "update nettteach_course set title = '".$title."' where id = ".$id;
			$db -> query($sql);
		}
	}
	if($xuhao != ''){
		$sql = "update nettteach_course set xuhao = '".$xuhao."' where id = ".$id;
		$db -> query($sql);
	}
//        if($sort != ''){
//		$sql = "update nettteach_course set sort = '".$sort."' where id = ".$id;
//		$db -> query($sql);
//	}
	if($introduction != ''){
		$sql = "update nettteach_course set introduction = '".$introduction."' where id = ".$id;
		$db -> query($sql);
	}
        if($outline != ''){
		$sql = "update nettteach_course set outline = '".$outline."' where id = ".$id;
		$db -> query($sql);
	}
        if($calendar != ''){
		$sql = "update nettteach_course set calendar = '".$calendar."' where id = ".$id;
		$db -> query($sql);
	}
	if($intotime == ''){
		$sql = "update nettteach_course set intotime = 0 where id = ".$id;
		$db -> query($sql);
	}
	else{
		$sql = "update nettteach_course set intotime = ".strtotime($intotime)." where id = ".$id;
		$db -> query($sql);
	}
	if($state != ''){
		$sql = "update nettteach_course set state = ".$state." where id = ".$id;
		$db -> query($sql);
	}
	else{
		$sql = "update nettteach_course set state = null where id = ".$id;
		$db -> query($sql);
	}
	if(fs_get_value("course_id") == ''){
		fs_showmsg("添加成功！");
	}
	else{
		fs_showmsg("修改成功！");
	}
}
//可以查询多个表信息的函数
function get_from_info($table,$str,$id){
	global $db;
	$sql = "select * from ".$table." where id = ".$id;
	$result = $db -> query($sql);
	$row = $db -> fetch_array($result);
	return $row[$str];
}
//根据XX表 的 id 返回系名和专业名 qzr
function fs_get_depart_major($table,$i)
{
	if ( $i=='' || is_null($i) || !is_numeric($i))	{return '无';break;}
	global $db;
	$sql	=	"select d.name as departmentname,m.name as majorname from ".$table." c,nettteach_department d,nettteach_major m where c.id=$i and c.majorid = m.id and c.departmentid = d.id ";
        $result =	$db -> query($sql);
	$row	=	$db -> fetch_array($result);
	return	$row['departmentname']."/".$row['majorname'];
}
?>

<!--
编缉课程结束
-->

<?php
function searchcourse()
{
?>
<!--<script type="text/javascript">
window.onload = function()
{
        CKEDITOR.replace( 'introduction',{ height : '150px', width:'600px',toolbar:'MyToolbar',fontSize_sizes:'16/16px' } );
        CKEDITOR.replace( 'outline',{ height : '150px', width:'600px',toolbar:'MyToolbar',fontSize_sizes:'16/16px' } );
        CKEDITOR.replace( 'calendar',{ height : '150px', width:'600px',toolbar:'MyToolbar',fontSize_sizes:'16/16px' } );
};
</script>-->
<table width="100%" border="0" cellpadding="0" cellspacing="0" >
		<tr>
		    <th class="CTitle">课程管理</th>
		</tr>	
		<tr>
			<td class="CPanel">
    <div class="SubMenu">
		<a href="admin_course.php?">课程管理</a> | 
		<a href="admin_course.php?do=addcourse">添加课程</a> | 
		<a href="admin_course.php?do=searchcourse">搜索课程</a>
	</div>
			</td>
		</tr>	
		<tr>
			<td>
			<div class="middle_right_list">
			<form name="searchform" method="post" action="?" class="CContent">
			<fieldset>
                                <legend>搜索课程</legend><label></label><br>
                                        <label><div align="right">主讲教师：</div></label>
                                        <select name="teacherid" style="width:120px;">
					    <option value="" >请选择主讲教师</option>
                                        <?php
                                                global  $db;
                                                $sql_teacher = "select * from nettteach_member where type in('1','2')";
                                                $result_teacher = $db -> query($sql_teacher);
                                                while($row_teacher = $db -> fetch_array($result_teacher))
                                                {       
                                                        echo  "<option  value = ".$row_teacher['id'].">".$row_teacher['realname']."</option>";
                                                }
                                        ?>
                                        </select><br/></br>
					<label><div align="right">课程名称：</div></label><input name="title" type="text" style="width:200px;" class="text" value="" /><br/><br/>
					<label><div align="right">课程分类：</div></label>
<!--					<select name="sort" style="width:120px;">
                                            <option value="" >请选择分类</option>
					    <option value="0" >普通课程</option>
                                            <option value="1" >推荐课程</option>
					    <option value="2" >精品课程</option>
					</select><br/></br>-->
                                        <label><div align="right">课程状态：</div></label>
					<select name="state" style="width:120px;">
					    <option value="">请选择状态</option>
					    <option value="1">不显示</option>
					    <option value="0">显示</option>
					</select><br/><br/>
					<label><div align="right">排列序号：</div></label><input name="xuhao" type="text" style="width:400px;" class="text" value=""><br/><br/>
					<label><div aling="right">课程介绍：</div></label><div style="padding-left:100px"><textarea name="introduction" style="width:600px;height:50px;"><?php echo($course_row['introduction']);?></textarea></div><br /><br/>
                                        <label><div aling="right">课程大纲：</div></label><div style="padding-left:100px"><textarea name="outline" style="width:600px;height:50px;"><?php echo($course_row['outline']);?></textarea></div><br /><br/>
                                        <label><div aling="right">课程日历：</div></label><div style="padding-left:100px"><textarea name="calendar" style="width:600px;height:50px;"><?php echo($course_row['calendar']);?></textarea></div><br /><br/>
					<div class="submit">
					<label></label><input type="submit" value="确认搜索"/>&nbsp;&nbsp;&nbsp;&nbsp;<a class="lj" href="admin_course.php">返回列表</a></div>
			</fieldset>
			</form>
			</div>
			</td>
		</tr>
	</table>
<?php
}
?>

<?php
//删除课程
function delete_course()
{
    global $db;
	$i=0;
	$course_id = $_POST['id'];
	while($course_id){
	    if($course_id[$i]==null) break;
		$sql = "delete from nettteach_course where id = ".$course_id[$i]."";
	    $db -> query($sql);
	    $i++;
	}
	fs_showmsg('删除成功!');
}
?>
